package com.flyco.modules.cms.service.impl;

import com.flyco.modules.cms.entity.CmsArticleV2;
import com.flyco.modules.cms.entity.CmsArticleV2Comment;
import com.flyco.modules.cms.mapper.CmsArticleV2CommentMapper;
import com.flyco.modules.cms.mapper.CmsArticleV2Mapper;
import com.flyco.modules.cms.service.ICmsArticleV2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 文章表
 * @Author: flyco
 * @Date:   2020-07-16
 * @Version: V1.0
 */
@Service
public class CmsArticleV2ServiceImpl extends ServiceImpl<CmsArticleV2Mapper, CmsArticleV2> implements ICmsArticleV2Service {

    @Autowired
    private CmsArticleV2Mapper cmsArticleV2Mapper;
    @Autowired
    private CmsArticleV2CommentMapper cmsArticleV2CommentMapper;

    @Override
    @Transactional
    public void saveMain(CmsArticleV2 cmsArticleV2, List<CmsArticleV2Comment> cmsArticleV2CommentList) {
        cmsArticleV2Mapper.insert(cmsArticleV2);
        for(CmsArticleV2Comment entity:cmsArticleV2CommentList) {
            //外键设置
            entity.setArticleId(cmsArticleV2.getId());
            cmsArticleV2CommentMapper.insert(entity);
        }
    }

    @Override
    @Transactional
    public void updateMain(CmsArticleV2 cmsArticleV2,List<CmsArticleV2Comment> cmsArticleV2CommentList) {
        cmsArticleV2Mapper.updateById(cmsArticleV2);

        //1.先删除子表数据
        cmsArticleV2CommentMapper.deleteByMainId(cmsArticleV2.getId());

        //2.子表数据重新插入
        for(CmsArticleV2Comment entity:cmsArticleV2CommentList) {
            //外键设置
            entity.setArticleId(cmsArticleV2.getId());
            cmsArticleV2CommentMapper.insert(entity);
        }
    }

    @Override
    @Transactional
    public void delMain(Long id) {
        cmsArticleV2CommentMapper.deleteByMainId(id);
        cmsArticleV2Mapper.deleteById(id);
    }

    @Override
    @Transactional
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for(Serializable id:idList) {
            cmsArticleV2CommentMapper.deleteByMainId(Long.valueOf(id.toString()));
            cmsArticleV2Mapper.deleteById(id);
        }
    }

}
